package com.xhwl.data.service.impl.personal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffExtraDefined;
import com.xhwl.data.dao.personal.IStaffExtraDao;
import com.xhwl.data.dao.personal.IStaffExtraDefinedDao;
import com.xhwl.data.service.personal.IStaffExtraDefinedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 14:01 2022/5/19
 */
@Service
public class StaffExtraDefinedServiceImpl extends ServiceImpl<IStaffExtraDefinedDao, StaffExtraDefined> implements IStaffExtraDefinedService {

    @Autowired
    private IStaffExtraDefinedDao staffExtraDefinedDao;

    @Override
    public ResultJson create(StaffExtraDefined staffExtraDefined) {
        ResultJson check = check(staffExtraDefined);
        if(!check.getState()){
            return check;
        }
        staffExtraDefinedDao.insert(staffExtraDefined);
        return ResultJson.success();
    }

    @Override
    public ResultJson update(StaffExtraDefined staffExtraDefined) {
        ResultJson check = check(staffExtraDefined);
        if(!check.getState()){
            return check;
        }
        StaffExtraDefined staffExtraDefined1 = staffExtraDefinedDao.selectById(staffExtraDefined.getId());
        if(null ==  staffExtraDefined1){
            return ResultJson.fail("不存在");
        }
        staffExtraDefinedDao.updateById(staffExtraDefined);
        return ResultJson.success();

    }

    @Override
    public Page<StaffExtraDefined> page(Integer enterpriseId, Page page) {
        QueryWrapper<StaffExtraDefined> staffExtraDefinedQueryWrapper = new QueryWrapper<>();
        staffExtraDefinedQueryWrapper.eq("enterprise_id", enterpriseId);
        return staffExtraDefinedDao.selectPage(page, staffExtraDefinedQueryWrapper);
    }


    private ResultJson check(StaffExtraDefined staffExtraDefined){
        if(null == staffExtraDefined.getEnterpriseId()){
            return ResultJson.fail("缺少企业id");
        }
        QueryWrapper<StaffExtraDefined> staffExtraDefinedQueryWrapper1 = new QueryWrapper<>();
        staffExtraDefinedQueryWrapper1.eq("name", staffExtraDefined.getName());
        staffExtraDefinedQueryWrapper1.eq("enterprise_id", staffExtraDefined.getEnterpriseId());
        if(null != staffExtraDefined.getId()){// 编辑
            staffExtraDefinedQueryWrapper1.ne("id", staffExtraDefined.getId());
        }else{
            QueryWrapper<StaffExtraDefined> staffExtraDefinedQueryWrapper = new QueryWrapper<>();
            staffExtraDefinedQueryWrapper.eq("enterprise_id", staffExtraDefined.getEnterpriseId());
            if(20 <= staffExtraDefinedDao.selectCount(staffExtraDefinedQueryWrapper)){
                return ResultJson.fail("超过数量限制");
            }

        }
        Integer integer = staffExtraDefinedDao.selectCount(staffExtraDefinedQueryWrapper1);
        if(integer > 0){
            return ResultJson.fail("名称重复");
        }
        return ResultJson.success();
    }
}
